#!/usr/bin/env python
# -*- coding: utf-8 -*-

from web.manager.oracle_manager import OracleManager
from web.models.stock_info import StockInfo

"""
StockInfo的dao类
"""


class StockInfoDao:

    def __init__(self):
        self.db = OracleManager()

    def find_by_name(self, name: str) -> StockInfo:
        """
        根据name查询记录
        """

        try:
            self.db.connect()
            _tuple = self.db.execute_query("select * from stock_info t where t.name_='" + name + "'")
            if _tuple is not None:
                stock_info = StockInfo(*_tuple[0])
                return stock_info
            else:
                return None
        finally:
            self.db.close()

    def find_by_code(self, code: str) -> StockInfo:
        """
        根据code查询记录
        """

        try:
            self.db.connect()
            _tuple = self.db.execute_query("select * from stock_info t where t.code_='" + code + "'")
            if _tuple is not None:
                stock_info = StockInfo(*_tuple[0])
                return stock_info
            else:
                return None
        finally:
            self.db.close()

    def find_all(self) -> list:
        """
        查询所有记录
        """

        try:
            self.db.connect()
            _tuple = self.db.execute_query("select * from stock_info")
            # _tuple = self.db.execute_query("select * from stock_info si_ where si_.delist=1")
            stock_info_list: list = list()
            if _tuple is not None:
                for row in _tuple:
                    stock_info = StockInfo(*row)
                    stock_info_list.append(stock_info)
                return stock_info_list
            else:
                return None
        finally:
            self.db.close()

    def update_name_by_code(self, code: str, name: str):
        """
        根据code，更新name
        """

        try:
            self.db.connect()
            self.db.execute_update("update stock_info set name_='" + name + "' where code_=" + code)
        finally:
            self.db.close()
